/**
 * 项目费用申请 - 编辑页面
 * @author liuwei
 * @date 2019/12/06
 */
define(['nzh'], function (nzh) {
    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            isDetail: false,
            form: {
                GWGLORIG_NAME: null,
                GWGLORIU_NAME: null,
                GWADL004: null,
                GWGL005_NAME: null,
                GWGL006_NAME: null,
                GWGL007_NAME: null,
                GWGL008_NAME: null,
                GWGL019_NAME: null,
                GWGL020_NAME: null,
                GWGL021_NAME: null
            },
            rules: {
                GWGL001: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGL002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGL003: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGL004: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGL005: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGL006: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGL009: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGL010: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGL011: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGL012: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGL013: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGL014: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGL015: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGL016: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGL017: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGL019: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWGL022: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGLDATE: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGLORIG: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWGLORIU: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],

                GWGLACTI: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ]
            },
            selectOptions: {
                gwgl009Options: app.datas_("gwgl009"),
                gwgl013Options: app.datas_("gwgl013")
            },

            uploadUrl: g_ctxPath + 'upload/files',
            file: '',
            fileList: [],
            fileList2: [],
            fileCount: 0,
            address: '',

            detailReadOnly: false
        },
        callObj,
        pageVue,
        moduleName = "项目费用",
        layOpenIndex
    ;

    var gwgi006Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwgi006_edit',
                _loadTplsFull: [
                    {
                        dom: 'common_open_window', tpl: 'common/_open_window'
                    }
                ]
            }, win);
        },
        editData: function () {
            app.ajax({
                xurl: 'crm/gwgi006/edit',
                xdata: vueData.form,
                loading: true,
                okmsg: moduleName + '信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        vueData.fileList = [];
                        vueData.fileList2 = [];

                        //同步更新缓存对应的值
                        callObj.update(result.xdata);
                        layer.close(layOpenIndex);
                    }
                }
            });
        },
        addData: function () {
            app.ajax({
                xurl: 'crm/gwgi006/add',
                xdata: vueData.form,
                loading: true,
                okmsg: moduleName + '信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        vueData.fileList = [];
                        vueData.fileList2 = [];
                        vueData.address = '';

                        app.pageModule('gwgi006').reloadTableCurr();
                        layer.close(layOpenIndex);
                    }
                }
            });
        },

        _call: function (tagData) {
            pageVue.$refs['form'].resetFields();
            layOpenIndex = tagData.layOpenIndex;
            vueData.fileList = [];
            vueData.fileList2 = [];
            vueData.address = '';
            vueData.fileCount = 0;
            if (tagData.detailReadOnly) {
                vueData.detailReadOnly = true;
            } else {
                vueData.detailReadOnly = false;
            }
            if (tagData.is_add) {
                vueData.isAdd = true;
                app.ajax({
                    xurl: 'crm/gwgi006/getAddData',
                    cb: function (result) {
                        if (result.ok) {
                            var resData = result.data.data;
                            app.extendUpLow({
                                target: vueData.form,
                                res: {
                                    GWGLORIU: resData.CURRENT_USER_ID,
                                    GWGLORIU_NAME: resData.CURRENT_USER_NAME,
                                    GWGLORIG: resData.CURRENT_ORG,
                                    GWGLORIG_NAME: resData.CURRENT_ORG_NAME,
                                    GWGLDATE: resData.CURRENT_DATE,
                                    GWGLACTI: "N"
                                },
                                reset: true
                            });
                        }
                    }
                });
            } else {
                vueData.isAdd = false;
                callObj = tagData.obj;
                vueData.detailReadOnly = tagData.detailReadOnly;
                if (callObj.data.GWGLACTI !== "N" && callObj.data.GWGLACTI !== "R") {
                    vueData.detailReadOnly = true;
                }
                if (callObj.data.GWGL025 != undefined && callObj.data.GWGL025 != "") {
                    if (callObj.data.GWGL025.length > 0) {

                        let list = callObj.data.GWGL025.split(',');
                        for (let i = 0; i < list.length; i++) {
                            vueData.fileList.push({
                                name: list[i], url: '/wissun-smfile/' + list[i]
                            });
                            vueData.fileList2.push({GWGL025: list[i]});
                        }
                    }
                }
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
            }
        },
        _ready: function () {
            vueData.form = app.fields_(['GWGL_T'], vueData.form);
            pageVue = new Vue({
                el: '#gwgi006_edit',
                data: vueData,
                methods: {
                    cancel: function () {
                        layer.close(layOpenIndex);
                    },
                    sure: function () {

                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if ((vueData.form.GWGL009 == '1' || vueData.form.GWGL009 == '3' || vueData.form.GWGL009 == '4')
                                    && (vueData.form.GWGL026 == null || vueData.form.GWGL026 == '' || vueData.form.GWGL018 == null || vueData.form.GWGL018 == '')) {
                                    app.showWarning("退还说明、退还时间必填！");
                                    return;
                                }
                                if (vueData.isAdd) {
                                    let flag = true;
                                    if (pageVue.$refs.upload2.uploadFiles.length > 0) {
                                        flag = false;
                                        pageVue.$refs.upload2.submit();
                                    }
                                    if (flag) {
                                        self.addData();
                                    }
                                } else { // 修改
                                    vueData.form.GWGL025 = '';
                                    // 将文件链接拼接起来
                                    let gwgl025s = [];
                                    for (var i = 0; i < vueData.fileList2.length; i++) {
                                        gwgl025s[i] = vueData.fileList2[i].GWGL025;
                                    }
                                    vueData.form.GWGL025 = gwgl025s.join(",");

                                    // 将没有改动过的文件从上传列表中删掉
                                    vueData.fileList2.forEach(function (v) {
                                        pageVue.$refs.upload2.uploadFiles.removeByMap({
                                            name: v.GWGL025
                                        });
                                    });

                                    let flag = true;
                                    if (pageVue.$refs.upload2.uploadFiles.length > 0) {
                                        flag = false;
                                        pageVue.$refs.upload2.submit();
                                    }
                                    if (flag) {
                                        self.editData();
                                    }
                                }
                            }
                        });
                    },
                    openSysuser: function (field) {
                        let title = "用户";
                        switch (field) {
                            case "GWGL007" :
                                title = "商务制作员";
                                break;
                            case "GWGL008" :
                                title = "商务审核员";
                                break;
                            case "GWGL019" :
                                title = "项目负责人";
                                break;
                            case "GWGL020" :
                                title = "负责人";
                                break;
                            case "GWGL021" :
                                title = "经办人";
                                break;
                        }
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_sysuser_id',
                                    columnList: [
                                        {label: '业务员编号', prop: 'ID', width: '150'},
                                        {label: '业务员名称', prop: 'NAME', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    switch (field) {
                                        case "GWGL007" :
                                            vueData.form.GWGL007_NAME = data.NAME;
                                            vueData.form.GWGL007 = data.ID;
                                            break;
                                        case "GWGL008" :
                                            vueData.form.GWGL008_NAME = data.NAME;
                                            vueData.form.GWGL008 = data.ID;
                                            break;
                                        case "GWGL019" :
                                            vueData.form.GWGL019_NAME = data.NAME;
                                            vueData.form.GWGL019 = data.ID;
                                            break;
                                        case "GWGL020" :
                                            vueData.form.GWGL020_NAME = data.NAME;
                                            vueData.form.GWGL020 = data.ID;
                                            break;
                                        case "GWGL021" :
                                            vueData.form.GWGL021_NAME = data.NAME;
                                            vueData.form.GWGL021 = data.ID;
                                            break;
                                    }
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择' + title,
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openOrg: function (field) {
                        let title = "组织";
                        if (field === "GWGL005") {
                            title = "参标单位"
                        } else {
                            title = "付款单位"
                        }
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_sysorg_biz',
                                    columnList: [
                                        {label: '公司编号', prop: 'ID', width: '150'},
                                        {label: '公司名称', prop: 'NAME', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    if (field === "GWGL005") {
                                        vueData.form.GWGL005 = data.ID;
                                        vueData.form.GWGL005_NAME = data.NAME;
                                    } else if (field === "GWGL006") {
                                        vueData.form.GWGL006 = data.ID;
                                        vueData.form.GWGL006_NAME = data.NAME;
                                    }
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择' + title,
                                area: ['600px', '470px']
                            }
                        });
                    },
                    openGwgl001: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwgc001',
                                    columnList: [
                                        {label: '投标ID', prop: 'GWGCID', width: '150'},
                                        {label: '招标编号', prop: 'GWGC001', width: '150'},
                                        {label: '项目名称', prop: 'GWGC022', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWGL023 = data.GWGCID;
                                    vueData.form.GWGL001 = data.GWGC001;
                                    vueData.form.GWGL002 = data.GWGC022;
                                    vueData.form.GWGL003 = data.GWGC024;
                                    // vueData.form.GWGL004 = data.GWGA003;
                                    // vueData.form.GWADL004 = data.GWADL004;
                                    vueData.form.GWGL007 = data.GWGC010;
                                    vueData.form.GWGL007_NAME = data.GWGC010_NAME;
                                    vueData.form.GWGL008 = data.GWGC011;
                                    vueData.form.GWGL008_NAME = data.GWGC011_NAME;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择招标信息',
                                area: ['600px', '465px']
                            }
                        });
                    },

                    openGwgl004: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config: {
                                    method: 'q_gwad002',
                                    searchParam: {
                                        GWAD001: "3"
                                    },
                                    columnList: [
                                        {label: '区域编号', prop: 'GWAD002', width: '150'},
                                        {label: '区域名称', prop: 'GWADL004', width: '150'},
                                        {label: '助记码', prop: 'GWADL005', width: '150'}
                                    ],
                                },
                                cb: function (data) {
                                    vueData.form.GWGL004 = data.GWAD002;
                                    vueData.form.GWADL004 = data.GWADL004
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择所属区域',
                                area: ['600px', '465px']
                            }
                        });
                    },

                    handleFilePreview: function (file) {
                        window.open('/smfile/' + file.name);
                    },
                    handleExceed: function (files, fileList) {
                        this.$message.warning("当前只允许选择一个文件，如果需要选择新的文件，请先删除之前的文件!");
                    },
                    handleFileRemove: function (file, fileList) {
                        if (!vueData.isAdd) {
                            vueData.fileList2.removeByMap({
                                GWGL025: file.name
                            });
                        }
                    },
                    beforeUploadFiles: function (file) {
                        const isJPG = (file.type === 'application/pdf'
                            || file.type === 'text/plain'
                            || file.type === 'application/msword' || file.type === 'application/vnd.ms-excel'
                            || file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
                            || file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif'
                            || file.type === 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
                        const isLt10M = file.size / 1024 / 1024 < 10;
                        if (!isJPG) {
                            this.$message.error('文件格式不对');
                        }
                        if (!isLt10M) {
                            this.$message.error('文件最多为10M');
                        }
                        return isJPG && isLt10M;
                    },
                    successUploadFiles: function (response) {
                        if (response.code === 200) {
                            if (vueData.form.GWGL025 === '' || vueData.form.GWGL025 == null) {
                                vueData.form.GWGL025 = response.data[0].name;
                            } else {
                                vueData.form.GWGL025 = vueData.form.GWGL025 + ',' + response.data[0].name;
                            }

                            vueData.fileCount++;
                            if (vueData.fileCount === pageVue.$refs.upload2.uploadFiles.length) {
                                pageVue.$message.success('文件上传成功');
                                if (vueData.isAdd) {
                                    self.addData();
                                } else {
                                    self.editData();
                                }
                                pageVue.$refs.upload2.clearFiles();
                            }
                        }
                    },
                },
                watch: {
                    'form.GWGL009': function (newValue) {
                        if (vueData.form.GWGL001 == null) {
                            app.showWarning("请先选择招标编号");
                            vueData.form.GWGL009 = null;
                        } else if (newValue === "2" || newValue === "3" || newValue === "4") {
                            app.ajax({
                                xurl: 'crm/gwgi006/getGwgc014',
                                xdata: {GWGL023: vueData.form.GWGL023},
                                cb: function (result) {
                                    if (result.ok) {
                                        let data = result.data.data;
                                        if (data.GWGC014 !== "Y") {
                                            vueData.form.GWGL009 = null;
                                            app.showWarning("此投标项目未维护中标信息，不可维护此类型费用！")
                                        }
                                    }
                                }
                            });
                        }
                    },
                    'form.GWGL015': function (newValue) {
                        if (newValue != null && newValue !== "") {
                            vueData.form.GWGL016 = nzh.cn.encodeB(parseFloat(newValue));
                        }
                    }
                },
                created: function () {
                    self._setEleIsReady('gwgi006_edit');
                }
            });
        },
        _lazy: function () {
            this._super();
        }
    });
    return new gwgi006Edit();
});